home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / conv / ansiconvert.lha / ANSIconvert.c < prev   
C/C++ Source or Header  |  1993-01-24  |  5KB  |  253 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <ctype.h>
  5.  
  6. #define TRUE  (0==0)
  7. #define FALSE (0!=0)
  8.  
  9. int lastread;
  10. int pos_x,pos_y;
  11. char cmd_s=FALSE,cmd_t=FALSE;
  12. FILE *filehandle;
  13.  
  14. void make_the_work(char *);
  15. unsigned int read_till_escape(char);
  16. unsigned int handle_csi(void);
  17. void handle_command(unsigned int);
  18. int readnum(int *);
  19. void new_position(int,int);
  20. void update_position(int);
  21.  
  22. void make_the_work(filename)
  23. char *filename;
  24. {
  25.     register unsigned int escresult;
  26.  
  27.     pos_x=pos_y=1;
  28.     if ((filehandle=fopen(filename,"r"))==NULL)
  29.     {
  30.         fprintf(stderr,"Unable to open file %s.\n",filename);
  31.         exit(30);
  32.     };
  33.     lastread=fgetc(filehandle);
  34.     while (lastread!=EOF)
  35.     {
  36.         if ((escresult=read_till_escape(TRUE))!=EOF)
  37.             handle_command(escresult);
  38.     };
  39.     putchar(10);
  40.     fclose(filehandle);
  41. }
  42.  
  43. unsigned int read_till_escape(do_write)
  44. /* reads the file till an escape sequence and returns the type of the escape */
  45. /* could be EOF, x<<8+y, style<<16 or 0 in case of an unsupported escape     */
  46. /* writes out non-escape characters if do_write is TRUE                      */
  47. char do_write;
  48. {
  49.     while ((lastread!=27) && (lastread!=EOF))
  50.     {
  51.         if ((do_write==TRUE) && (isprint(lastread) || (lastread=='\n')))
  52.         {
  53.             putchar(lastread);
  54.             update_position(lastread);
  55.         };
  56.         lastread=fgetc(filehandle);
  57.     };
  58.     if (lastread==EOF)
  59.         return(EOF);
  60.     else
  61.     {
  62.         lastread=fgetc(filehandle);
  63.         if (lastread=='[')
  64.             return(handle_csi());
  65.         else
  66.         {
  67.             lastread=fgetc(filehandle);
  68.             return(0);
  69.         };
  70.     };
  71. }
  72.  
  73. unsigned int handle_csi(void)
  74. /* last characters were CSI : read the command, reply result. at exit time,  */
  75. /* lastread must be the one following the control sequence                   */
  76. {
  77.     unsigned int resu;
  78.     int ch;
  79.     int num1,num2;
  80.  
  81.     ch=fgetc(filehandle);
  82.     if (isdigit(ch))
  83.     {
  84.         num1=readnum(&ch);
  85.         if (ch==';')
  86.         {
  87.             ch=fgetc(filehandle);
  88.             num2=readnum(&ch);
  89.             if (ch=='H')
  90.                 resu=num1+(num2<<8);
  91.             else
  92.             {
  93.                 while (!isalpha(ch))
  94.                     ch=fgetc(filehandle);
  95.                 resu=0;
  96.             };
  97.         }
  98.         else
  99.             resu=(num1+1)<<16;
  100.     }
  101.     else
  102.         resu=0;
  103.     lastread=fgetc(filehandle);
  104.     return(resu);
  105. }
  106.  
  107. int readnum(ch)
  108. int *ch;
  109. {
  110.     int resu=0;
  111.  
  112.     while (isdigit(*ch))
  113.     {
  114.         resu*=10;
  115.         resu+=*ch-'0';
  116.         *ch=fgetc(filehandle);
  117.     };
  118.     return(resu);
  119. }
  120.  
  121. void handle_command(cmd)
  122. unsigned int cmd;
  123. {
  124.     if (cmd!=0)
  125.     {
  126.         if ((cmd&0xffff)!=0)
  127.             new_position(cmd>>8,cmd & 0xff);
  128.         else
  129.             if (cmd_t==TRUE)
  130.                 printf("\033[%dm",(cmd>>16)-1);
  131.     };
  132. }
  133.  
  134. void new_position(x,y)
  135. int x;
  136. int y;
  137. {
  138.     register unsigned int resultat=!EOF;
  139.  
  140.     if ((cmd_s==TRUE)&&(y>=24))
  141.     {
  142.         while (resultat!=EOF)
  143.         {
  144.             resultat=read_till_escape(FALSE);
  145.             if ((resultat & 0xffff)==0)
  146.                 handle_command(resultat);
  147.             else
  148.                 if ((resultat & 0xff)<24)
  149.                 {
  150.                     y=resultat & 0xff;
  151.                     x=resultat>>8;
  152.                     resultat=EOF;
  153.                 };
  154.         };
  155.     };
  156.     if (y!=pos_y)
  157.     {
  158.         if (y<pos_y)
  159.         {
  160.             putchar(12);
  161.             putchar(10);
  162.             pos_y=2;
  163.         }
  164.         else
  165.             putchar(10);
  166.         pos_y=y;
  167.         pos_x=1;
  168.     };
  169.     if (x!=pos_x)
  170.     {
  171.         if (x<pos_x)
  172.         {
  173.             putchar(10);
  174.             pos_x=1;
  175.         };
  176.         while (pos_x++<x)
  177.             putchar(' ');
  178.         pos_x--;
  179.     };
  180. }
  181.  
  182. void update_position(ch)
  183. int ch;
  184. {
  185.     if ((ch==13)||(ch==10)||(ch==12))
  186.         pos_x=1;
  187.     if (ch==10)
  188.         pos_y++;
  189.     if (ch==12)
  190.         pos_y=1;
  191.     if (ch==8)
  192.         pos_y=(pos_y==1)?1:((pos_y+7)&(-8));
  193.     if (ch>=32)
  194.         pos_x++;
  195. }
  196.  
  197. main(argc,argv)
  198. int argc;
  199. char *argv[];
  200. {
  201.     int i;
  202.  
  203.     fprintf(stderr,\
  204. "\033[1mANSIconvert\033[0m ©1993 by Jean-François Stenuit\n\
  205. Prints out the contents of an ANSI formated file as bare text.\n");
  206.     if (argc==0)
  207.     {
  208.         fprintf(stderr,"Runs from the shell only\n");
  209.         exit(30);
  210.     };
  211.     if (argc<2)
  212.     {
  213.         fprintf(stderr,"Usage: ANSIconvert [options] InputFile\n");
  214.         exit(30);
  215.     };
  216.     if ((argc==2) && (strcmp(argv[1],"?")==0))
  217.     {
  218.         fprintf(stderr,"\
  219. prints to the standard output with the following options :\n\
  220. \t -s : strips out the status lines (24 and further)\n\
  221. \t -t : does not strip out the characters attributes (bold, ...)\n\
  222. This program if shareware, if you use it, please send $10 to :\n\
  223. \t\tStenuit Jean-François\n\
  224. \t\tAvenue de Falichamp, 10\n\
  225. \t\tB - 5100 JAMBES\n\
  226. \t\tBELGIUM\n");
  227.         exit(0);
  228.     };
  229.     if (argc==3)
  230.     {
  231.         if (argv[1][0]!='-')
  232.         {
  233.             fprintf(stderr,"Invalid argument : %s\n",argv[1]);
  234.             exit(30);
  235.         }
  236.         else
  237.             for (i=1;argv[1][i]!='\0';i++)
  238.                 switch(argv[1][i])
  239.                 {
  240.                     case 's' : cmd_s=TRUE;
  241.                                break;
  242.                     case 't' : cmd_t=TRUE;
  243.                                break;
  244.                     default :
  245. fprintf(stderr,"Invalid option : %c\n",argv[1][i]);
  246.                                break;
  247.                 };
  248.         argv[1]=argv[2];
  249.     };
  250.     make_the_work(argv[1]);
  251.     return(0);
  252. }   
  253.